#!/bin/bash
#

#判断是否存在密钥对
if [ -f  /root/.ssh/id_rsa ]; then
 rm -rf /root/.ssh/id_rsa*
fi
ssh-keygen -t rsa -f "/root/.ssh/id_rsa" -P "" &> /dev/null
#向目标主机发送公钥
if [ -f /root/.ssh/known_hosts ]; then
  rm -rf /root/.ssh/known_hosts
fi

for i in 142; do
/usr/bin/expect << eof
set timeout 10
spawn ssh-copy-id root@192.168.137.$i
expect "(yes/no)?"
send "yes\n"
expect "password:"
send "123456\n"
expect eof
eof
done

echo "------------------"
#验证
for i in 142; do
  ssh root@192.168.137.$i hostname
done

